package com.hlkj.baojin.dao.repository;

import com.hlkj.baojin.constant.BaseRepository;
import com.hlkj.baojin.entity.Duty;
import com.hlkj.baojin.entity.DutyLog;
import com.hlkj.baojin.entity.dto.DutyLogSearchDto;
import com.hlkj.baojin.utils.MyLists;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HULIN
 * @created with: baojin
 * @package com.hlkj.baojin.dao.repository
 * @description:
 * @date 2019/1/30 12:50
 * @modified By:
 * @Copyright © 2018 HAILIANG Info. Tech Ltd. All rights reserved.
 */
@Component
public class DutyLogRepositoty extends BaseRepository<DutyLog> {

    public DutyLogRepositoty(MongoTemplate mongoTemplate) {
        super(mongoTemplate);
    }

    public List<DutyLog> getDutyLog(DutyLogSearchDto dutyLogSearchDto, List<String> stringList) {

        Query query = new Query();
        if (dutyLogSearchDto.getTimeStart() != null && dutyLogSearchDto.getTimeEnd() != null) {
            query.addCriteria(Criteria.where("deadlineDate").gte(dutyLogSearchDto.getTimeStart()).lt(dutyLogSearchDto.getTimeEnd()));
        }
        if(dutyLogSearchDto.getDutyName() !=null && MyLists.notNullAndEmpty(stringList)) {
            query.addCriteria(Criteria.where("dutyName").in(stringList));
        } else if(dutyLogSearchDto.getDutyName() !=null && !MyLists.notNullAndEmpty(stringList)) {
            return new ArrayList<>();
        }
        query.with(new Sort(Sort.Direction.DESC, "time"));
        query.with(new Sort(Sort.Direction.DESC, "timeStart"));


        List<DutyLog> dutyLogList = mongoTemplate.find(query, DutyLog.class);

        return dutyLogList;
    }
}
